using System;
using System.Collections.Generic;
using CodeStory.Core.ObjectEntity;

namespace CodeStory.Core.Collection
{
	public class ResourceCollection
	{
		private Dictionary<string, RoleResourceCollection> dict;

		public ResourceCollection() : this(0x20) {
		}

		public ResourceCollection(int capacity) {
			dict = new Dictionary<string, RoleResourceCollection>(capacity);
		}

		public bool ContainsKey(string key) {
			return dict.ContainsKey(key);
		}

		public RoleResourceCollection this[string key] {
			get { return dict[key]; }
			set { dict[key] = value; }
		}

		public bool Remove(string key) {
			return dict.Remove(key);
		}
	}

	public class RoleResourceCollection
	{
		private Dictionary<Guid, RoleResourceObject> dict;

		public RoleResourceCollection() {
			dict = new Dictionary<Guid, RoleResourceObject>(0x10);
		}

		public bool GetPermission(long action, IEnumerable<Guid> roles) {
			long p = 0;
			foreach (Guid id in roles) {
				if (dict.ContainsKey(id)) {
					p |= dict[id].Permission;
				}
			}
			return (p & action) > 0;
		}

		public RoleResourceObject this[Guid key] {
			get { return dict[key]; }
			set { dict[key] = value; }
		}
	}
}